const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userinfo: {},
        uId: 0,
        list: [],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('onload');
        this.setData({
            uId: app.globalData.uId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log('ready');
        var that = this;
        const userinfo = wx.getStorageSync("userinfo");
        this.setData({
            userinfo
        });
        wx.request({
            url: 'http://127.0.0.1:8080/superadmin/listgroup',
            data: {
                uId: that.data.uId
            },
            method: 'GET',
            success: function (res) {
                var list = res.data.grouplist;

                for (var i = 0; i < list.length; i++) {
                    if (list[i].personalGroupAccountList[0].category == null) {
                        list[i].personalGroupAccountList[0].category = '';
                    } else {
                        list[i].personalGroupAccountList[0].category += '发布了一条新的消费记录';
                    }
                    if (list[i].personalGroupAccountList[0].pDate == null) {
                        list[i].personalGroupAccountList[0].pDate = '';
                    }
                }
                that.setData({
                    list: list
                })

            }
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('onshow');
        var that = this;
        wx.request({
            url: 'http://127.0.0.1:8080/superadmin/listgroup',
            data: {
                uId: that.data.uId
            },
            method: 'GET',
            success: function (res) {
                var list = res.data.grouplist;

                for (var i = 0; i < list.length; i++) {
                    if (list[i].personalGroupAccountList[0].category == null) {
                        list[i].personalGroupAccountList[0].category = '';
                    } else {
                        list[i].personalGroupAccountList[0].category += '发布了一条新的消费记录';
                    }
                    if (list[i].personalGroupAccountList[0].pDate == null) {
                        list[i].personalGroupAccountList[0].pDate = '';
                    }
                }
                that.setData({
                    list: list
                })


            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {


    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {


    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    add: function (e) {
        var that = this;
        wx.showModal({
            title: '请输入查找的群号',
            editable: true,
            cancelColor: 'cancelColor',
            success(res) {
                if (res.confirm) {
                    var gid = res.content;
                    wx.request({
                        url: 'http://127.0.0.1:8080/superadmin/addgroup',
                        data: {
                            uId: that.data.uId,
                            gId: gid
                        },
                        method: 'GET',
                        success: function (res) {
                            var result = res.data.addgroup;
                            if (result == 0) {
                                wx.showToast({
                                    title: '没有查找到该群',
                                    icon: 'none',
                                    duration: 1000,
                                })
                            } else if (result == -1) {
                                wx.showToast({
                                    title: '您已经在该群中',
                                    icon: 'none',
                                    duration: 1000,
                                })
                            } else {
                                wx.showToast({
                                    title: '成功加入该群',
                                    icon: 'success',
                                    duration: 1000,
                                    mask: true
                                })
                            }
                            that.onReady();
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        })
    },
    create: function (e) {
        var that = this;
        wx.showModal({
            title: '请输入要创建的群名',
            editable: true,
            cancelColor: 'cancelColor',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定');
                    var gName = res.content;
                    wx.request({
                        url: 'http://127.0.0.1:8080/superadmin/creategroup',
                        data: {
                            uId: that.data.uId,
                            gName: gName
                        },
                        method: 'GET',
                        success: function (res) {
                            wx.showToast({
                                title: '创建成功',
                                icon: 'succes',
                                duration: 1000,
                                mask: true
                            })
                            that.onReady();
                        },
                        fail: function (res) {
                            console.log('fail');
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        })
    }
})